package com.addcn.caruimodule.viewpager;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.viewpager.widget.ViewPager;

/**
 * Created by 10464 on 2017/2/16.
 * 垂直滾動viewpager
 */

public class CustomVerViewPager extends ViewPager {
    private boolean onscroll = false;
    public CustomVerViewPager(Context context) {
        this(context, null);
    }

    public CustomVerViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setPageTransformer(false, new DefaultTransformer());
    }

    private MotionEvent swapTouchEvent(MotionEvent event) {
        float width = getWidth();
        float height = getHeight();
        float swappedX = (event.getY() / height) * width;
        float swappedY = (event.getX() / width) * height;
        event.setLocation(swappedX, swappedY);
        return event;
    }

    public boolean isOnscroll() {
        return onscroll;
    }

    public void setOnscroll(boolean onscroll) {
        this.onscroll = onscroll;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (onscroll){
            return false;
        }else{
            boolean intercept = super.onInterceptTouchEvent(swapTouchEvent(event));
            swapTouchEvent(event);
            return intercept;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return super.onTouchEvent(swapTouchEvent(ev));
    }

}
